#include "FIFO.h"
#include "linkedList.h"
#include <sys/times.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

void fifo(struct jobNode **ready, struct jobNode **job, clock_t start, int *alarm_policy){

    struct jobNode *temp_job;
    struct jobNode *temp_ready;
    //struct jobNode *ready;
    //struct jobNode *job;
    clock_t end;
    struct tms tmsend;
    static long clktck = 0;
    //int dirtybit = 0;
    //temp_job = job;
    //ready = *ready_s;
    //job = *job_s;
    temp_ready = *ready;
    end = times(&tmsend);

    //get system clock tick per second
    if (clktck == 0)
        if ((clktck = sysconf(_SC_CLK_TCK)) < 0) {
            perror("sysconf error");
            exit(-1);
        }


    //

    while(*job != NULL && (*job)->arrivalTime <= (int)((end-start)/clktck)){
        if(temp_ready != NULL){
            while(temp_ready->ready_next != NULL)
                temp_ready = temp_ready->ready_next;
            temp_ready->ready_next = *job;
        }else{
            *ready = *job;
            temp_ready = *ready;
        }
        *job = (*job)->next;
    }

    if (*ready != NULL && (*ready)->timeRemain <= 0 && (*ready)->runinfinity == 0)
        *ready = (*ready)->ready_next;
    else if (*ready != NULL && (*ready)->timeRemain == 0 && (*ready)->runinfinity == 1)
            *ready = (*ready)->ready_next;

    *alarm_policy = 1;
    if(*ready!=NULL)
        if ((*ready)->runinfinity == 1)
             *alarm_policy = 1;
        else
            *alarm_policy = (int)(*ready)->timeRemain;

}
